home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / etc / apm / scripts.d / alsa next >
Encoding:
Text File  |  2006-08-29  |  5.7 KB  |  230 lines

  1. #!/bin/sh
  2. #
  3. # alsa-base control script
  4. #
  5. ### BEGIN INIT INFO
  6. # Provides:           alsa
  7. # Required-Start:    mountall
  8. # Required-Stop:     mountall
  9. # Should-Start:
  10. # Should-Stop:
  11. # Default-Start:     S
  12. # Default-Stop:      0 1 6
  13. # Short-Description: Script to unload and load ALSA modules
  14. # Description: Used to load and unload ALSA modules and
  15. #              restore and store mixer levels. There is no
  16. #              longer any need to run this script on bootup
  17. #              or shutdown. It ships in the apm script dir
  18. #              as /etc/apm/scripts.d/alsa .
  19. ### END INIT INFO
  20.  
  21. set -e
  22.  
  23. # Exit if alsa-utils package is not installed
  24. [ -x /sbin/alsactl ] || exit 0
  25.  
  26. MYNAME=/etc/apm/scripts.d/alsa
  27. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  28.  
  29. # Default values of variables in /etc/default/alsa
  30. force_unload_modules_before_suspend=""
  31.  
  32. [ -f /etc/default/alsa ] && . /etc/default/alsa
  33.  
  34. # $* MESSAGE
  35. warn() { echo "${MYNAME}: Warning: $* " >&2 ; }
  36.  
  37. #
  38. # Attempt to create /var/run/alsa if it is absent.
  39. # Return true if /var/run/alsa exists after this attempt,
  40. # otherwise false.
  41. #
  42. check_run_dir()
  43.     [ -d /var/run/alsa ] && return 0
  44.     # We have no business creating /var/run if it doesn't exist
  45.     if ! [ -d /var/run ] ; then
  46.         warn "Could not create /var/run/alsa/ because /var/run/ is not present."
  47.         return 1
  48.     fi
  49.     if ! mkdir --mode=755 /var/run/alsa ; then
  50.         warn "Failed to create /var/run/alsa/."
  51.         return 1
  52.     fi
  53.     [ -d /var/run/alsa ] && return 0
  54.     return 1
  55. }
  56.  
  57. echo_procs_using_sound()
  58. {
  59.     echo $( \
  60.         lsof +D /dev -F rt \
  61.         | awk '/^p/ {pid=$1} /^t/ {type=$1} /^r0x(74|e)..$/ && type == "tCHR" {print pid}' \
  62.         | cut -c 2- \
  63.         | uniq \
  64.     )
  65. }
  66.  
  67. # $* [PID]...
  68. echo_with_command_names()
  69. {
  70.     [ "$1" ] || return 0
  71.     echo $( \
  72.         ps --no-headers -o "%p %c" "$@" \
  73.         | sed -e 's/\([0-9][0-9]*\) \(.*\)/\1(\2)/' \
  74.     )
  75. }
  76.  
  77. kill_procs_using_sound()
  78. {
  79.     procs_using_sound="$(echo_procs_using_sound)"
  80.     if [ "$procs_using_sound" ] ; then
  81.         echo -n "Terminating processes:"
  82.         for attempt in 1 2 3 4 ; do
  83.             echo -n " ${procs_using_sound}"
  84.             kill $procs_using_sound || :
  85.             sleep 1
  86.             procs_using_sound="$(echo_procs_using_sound)"
  87.             [ "$procs_using_sound" ] || break
  88.         done
  89.         # Either no more procs using sound or attempts ran out
  90.         if [ "$procs_using_sound" ] ; then
  91.             echo -n " (with SIGKILL:) ${procs_using_sound}"
  92.             kill -9 $procs_using_sound || :
  93.             sleep 1
  94.         fi
  95.         procs_using_sound="$(echo_procs_using_sound)"
  96.         if [ "$procs_using_sound" ] ; then
  97.             echo " (failed: processes still using sound devices: $(echo_with_command_names $procs_using_sound))."
  98.             return 1
  99.         fi
  100.         echo "."
  101.     fi
  102.     return 0
  103. }
  104.  
  105. # $* MODULE-NAME [MODULE-NAME]... | "all"
  106. unload_modules()
  107. {
  108.     procs_using_sound="$(echo_procs_using_sound)"
  109.     if [ "$procs_using_sound" ] ; then
  110.         warn "Processes using sound devices: $(echo_with_command_names $procs_using_sound)."
  111.     fi
  112.     if check_run_dir ; then
  113.         :> /var/run/alsa/modules-removed
  114.     else
  115.         warn "Not keeping list of removed modules because /var/run/alsa is absent.
  116. It will not be possible automatically to reload these modules."
  117.     fi
  118.     echo -n "Unloading ALSA sound driver modules:"
  119.     [ -d /proc/asound ] || { echo " (none loaded)." ; return 0 ; }
  120.     echo_snd_modules_loaded()
  121.     {
  122.         lsmod \
  123.         | sed -n -e 's/^\(snd[-_][^[:space:]]*\)[[:space:]].*/\1/p' \
  124.         | sed -e 's/_/-/g'
  125.     }
  126.     for FSMBS in $* ; do
  127.         MODULES_TO_REMOVE=""
  128.         SND_MODULES_LOADED="$(echo_snd_modules_loaded)"
  129.         case "$FSMBS" in
  130.           all)
  131.             MODULES_TO_REMOVE="$SND_MODULES_LOADED"
  132.             ;;
  133.           snd_*|snd-*)
  134.             FSMBS="$(echo "$FSMBS" | sed -e 's/_/-/g')"
  135.             for M in $SND_MODULES_LOADED ; do
  136.                 if [ "$FSMBS" = "$M" ] ; then
  137.                     MODULES_TO_REMOVE="$FSMBS"
  138.                     break
  139.                 fi
  140.             done
  141.             ;;
  142.         esac
  143.         [ "$MODULES_TO_REMOVE" ] || continue
  144.         echo "$MODULES_TO_REMOVE" >> /var/run/alsa/modules-removed
  145.         for M in $MODULES_TO_REMOVE ; do
  146.             echo -n " ${M}"
  147.             modprobe -r "$M" >/dev/null 2>&1 || :
  148.         done
  149.     done
  150.     if [ -f /var/run/alsa/modules-removed ] ; then
  151.         MODULES_STILL_LOADED="$(echo_snd_modules_loaded | grep -F -f /var/run/alsa/modules-removed)"
  152.         MODULES_STILL_LOADED="$(echo $MODULES_STILL_LOADED)"
  153.     else
  154.         MODULES_STILL_LOADED=""
  155.     fi
  156.     if [ "$MODULES_STILL_LOADED" ] ; then
  157.         echo " (failed: modules still loaded: ${MODULES_STILL_LOADED})."
  158.         return 1
  159.     else
  160.         echo "."
  161.         return 0
  162.     fi
  163. }
  164.  
  165. # $* MODULE-NAME [MODULE-NAME]... | "all"
  166. force_unload_modules()
  167. {
  168.     kill_procs_using_sound || :
  169.     unload_modules "$@" || return 1
  170.     return 0
  171. }
  172.  
  173. load_unloaded_modules()
  174. {
  175.     LUM_RETURNSTATUS=0
  176.     MODULES_TO_LOAD=""
  177.     [ -d /var/run/alsa ] || mkdir -p /var/run/alsa
  178.     echo -n "Loading ALSA sound driver modules:"
  179.     [ -f /var/run/alsa/modules-removed ] && MODULES_TO_LOAD="$(echo $(cat /var/run/alsa/modules-removed))"
  180.     [ "$MODULES_TO_LOAD" ] || { echo " (none to reload)." ; return $LUM_RETURNSTATUS ; }
  181.     echo -n " $MODULES_TO_LOAD"
  182.     for MDL in $MODULES_TO_LOAD ; do
  183.         modprobe $MDL || LUM_RETURNSTATUS=1
  184.     done
  185.     case "$LUM_RETURNSTATUS" in
  186.       0) echo "." ;;
  187.       *) echo " (failed)." ;;
  188.     esac
  189.     return $LUM_RETURNSTATUS
  190. }
  191.  
  192. case "$1" in
  193.   unload)
  194.     unload_modules all || exit $?
  195.     ;;
  196.   reload)
  197.     EXITSTATUS=0
  198.     unload_modules all || EXITSTATUS=1
  199.     load_unloaded_modules || EXITSTATUS=1
  200.     exit $EXITSTATUS
  201.     ;;
  202.   force-unload)
  203.     force_unload_modules all || exit $?
  204.     ;;
  205.   force-reload)
  206.     EXITSTATUS=0
  207.     force_unload_modules all || EXITSTATUS=1
  208.     load_unloaded_modules || EXITSTATUS=1
  209.     exit $EXITSTATUS
  210.     ;;
  211.   suspend)
  212.     case "$force_unload_modules_before_suspend" in
  213.       ""|false) : ;;
  214.       all|true) /sbin/alsactl store && force_unload_modules all || exit $? ;;
  215.       *) /sbin/alsactl store && force_unload_modules $force_unload_modules_before_suspend || exit $? ;;
  216.     esac
  217.     ;;
  218.   resume)
  219.     case "$force_unload_modules_before_suspend" in
  220.       ""|false) : ;;
  221.       *) load_unloaded_modules && /sbin/alsactl restore || exit $? ;;
  222.     esac
  223.     ;;
  224.   *)
  225.     echo "Usage: $MYNAME {unload|reload|force-unload|force-reload|suspend|resume}" >&2
  226.     exit 3
  227.     ;;
  228. esac
  229.